package work.xiaohong.saolei.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import work.xiaohong.model.Result;
import work.xiaohong.model.StatusCode;
import work.xiaohong.saolei.pojo.Versions;
import work.xiaohong.saolei.service.SaoleiVersionsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "SaoleiVersion接口文档")
@RestController
@RequestMapping("/saoleiVersion")
public class SaoleiVersionController {

    @Autowired
    private SaoleiVersionsService saoleiVersionService;

    /***
     * 条件分页查询
     * @param saoleiVersions
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "SaoleiVersion对象",required = true) Versions saoleiVersions, @PathVariable int page, @PathVariable  int size){
        //调用SaoleiVersionService实现分页条件查询SaoleiVersion
        Page pageInfo = saoleiVersionService.findPage(saoleiVersions, page, size);
        return new Result<Page>(true, StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 分页查询
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SaoleiVersionService实现分页查询SaoleiVersion
        Page pageInfo = saoleiVersionService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 新增SaoleiVersion数据
     * @param saoleiVersions
     * @return
     */
    @Operation(summary = "添加SaoleiVersion",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "SaoleiVersion对象",required = true) Versions saoleiVersions){
        //调用SaoleiVersionService实现添加SaoleiVersion
        saoleiVersionService.add(saoleiVersions);
        return new Result(true,StatusCode.OK.getCode(),"添加成功", saoleiVersions);
    }

    /**
     * 获取最新版本
     * @param name
     * @return
     */
    @Operation(summary = "获取最新版本",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @GetMapping("/lastest")
    public Result getLastVersion(@RequestParam @Parameter(name = "软件名称",required = true) String  name){
        Versions saoleiVersions = saoleiVersionService.getLastVersion(name);
        if(saoleiVersions != null){
            return new Result(true,StatusCode.OK.getCode(),"获取成功", saoleiVersions);
        }else {
            return new Result(true,StatusCode.ERROR.getCode(),"获取失败");
        }

    }


}
